package fun.pengzh.gui.demo;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author fun.pengzh
 * @class fun.pengzh.gui.demo.JScene
 * @desc
 * @since 2022-02-01
 */
public class JScene extends JFrame {
    private static Graphics g;
    private int x, length, width;
    private int[][] a = new int[100][2]; // 障碍物的坐标
    private int[] start = new int[2];
    private int[] end = new int[2];

    public void paint(Graphics g) {
    }

    public JScene(int x, int length, int width) {
        this.x = x;
        this.length = length;
        this.width = width;
        Random rand = new Random();
        this.start[0] = rand.nextInt(length + 1);
        this.start[1] = rand.nextInt(length + 1);
        this.end[0] = rand.nextInt(length + 1);
        this.end[1] = rand.nextInt(length + 1);

    }

    public JScene() {

    }

    public void initUI() {
        this.setTitle("A星寻路算法");
        this.setSize(800, 800);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        g = this.getGraphics();
        g.setColor(Color.green);
        g.fillRect(x + start[0] * width, x + start[1] * width, width, width);
        g.setColor(Color.red);
        g.fillRect(x + end[0] * width, x + end[1] * width, width, width);
        g.setColor(Color.black);
        this.paintsolid(a);
//        ArrayList<int[]> list = AStarSearch.AStarSearching(a, x, length, width, start, end);
        ArrayList<int[]> list = BFS.BFSSearching(a, x, length, width, start, end);
        for (int i = 0; i < list.size() - 1; i++) {
            g.drawLine(x + list.get(i)[0] * width + width / 2, x + width * list.get(i)[1] + width / 2, x + width * list.get(i + 1)[0] + width / 2,
                    x + width * list.get(i + 1)[1] + width / 2);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void paintsolid(int[][] a) {
        Random rand = new Random();
        for (int i = 0; i < a.length; i++) {
            a[i][0] = (rand.nextInt(length + 1));
            a[i][1] = (rand.nextInt(length + 1));
            g.fillRect(a[i][0] * width + x, a[i][1] * width + x, width, width);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new JScene(100, 30, 20).initUI();

    }

}
